﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SlimDX;
using Device = SlimDX.Direct3D11.Device;
using Vxl.Utils;
using Vxl.Drawables;
using Vxl.Cameras;
using Vxl.Utils.Input;
using System.Windows.Forms;

namespace Vxl
{
    public class VxlEngine
    {        
        private bool usingMouse = true;
        private bool usingKeyboard = true;
        private List<VxlObject> vxlObjects = new List<VxlObject>();

        public int VxlObjectCount {
            get { return vxlObjects.Count; }
        }

        public bool UsingKeyboard{
            get{ return usingKeyboard; }
            set{ usingKeyboard = value; }
        }

        public bool UsingMouse{
            get{ return usingMouse; }
            set{ usingMouse = value; }
        }
        
        public VxlEngine(Device device, Control control)
        {
            Globals.Device = device;
            InputManager.Initialize(control);
        }

        public void CleanUp()
        {
            Globals.Device.Dispose();
            foreach (VxlObject o in vxlObjects)
            {
                o.CleanUp();
            }
        }

        public void AddVxlObject(ref VxlObject vxlObject)
        {
            vxlObject.Initialize();
            vxlObjects.Add(vxlObject);
        }

        public void RemoveVxlObject(ref VxlObject vxlObject){
            this.vxlObjects.Remove(vxlObject);
            vxlObject.CleanUp();
        }

        public void Update(float dt)
        {
            if(Globals.ActiveCamera == null)
            {
                // set a basic static camera which looks at (0,0,0)
                SetCamera(new BaseCamera(new Vector3(2.5f, 2.5f, 2.5f), Vector3.Zero));
            }
            Globals.ActiveCamera.Update(dt);
            
            foreach (VxlObject o in vxlObjects)
                o.Update(dt);
        }

        public void Draw(float dt)
        {

            if (vxlObjects.Count == 0)
                return;

            VxlObject.SetPerFrame();

            foreach (VxlObject o in vxlObjects)
                o.Draw();
        }

        public void SetCamera(BaseCamera camera)
        {
            Globals.ActiveCamera = camera;
        }
    }
}
